<template>
  <GlobalContainer className="!bg-[#F3F3F3]" :backgroundImage="`${OSS_URL}/images/baoxiu/head_bg.png`">
    <GlobalHeader ref="headerRef" title="历史工单" showBack :backgroundImage="`${OSS_URL}/images/baoxiu/head_bg.png`" :fixed="true" />
    <div class="px-24">
      <div class="status-bar-container fixed" :style="{ top: `${headerHeight}` }">
        <div
          class="status-bar-item"
          v-for="(item, index) in statusBarList"
          :key="index"
          :class="statusBarActive === item.value && 'status-bar-item-active'"
          @tap="statusBarChange(item.value)"
        >
          {{ item.name }}
        </div>
      </div>

      <div class="list-c" :style="{ marginTop: '100rpx' }">
        <div class="item-c mb-20 p-32 pr-44" v-for="item in orderList" :key="item" @tap="toPageHandle(item.id)">
          <div class="line1">
            <span class="text-30 leading-45 mr-4" :style="{ color: typeColorMap[item.type] }">{{ typeMap[item.type] }}</span>
            <div class="status ml-auto" :class="statusClassMap[item.status] || 'status-done'">{{ statusMap[item.status] }}</div>
            <image class="ml-20 size-32" src="../../static/images/jiaofei_right_icon.png" />
          </div>
          <div class="text-28 leading-42 font-normal text-[#909399]">{{ item.orderNo }}</div>

          <div class="line2 mt-20">
            <image class="size-100 mr-20 shrink-0" :src="item.images || `${OSS_URL}/images/default.png`" />
            <div class="flex flex-col overflow-hidden">
              <span class="text-28 leading-46 text-wrap font-normal text-[#606266]">{{ cutText(item.content) }}</span>
              <span class="text-30 leading-46 mt-10 text-[#303133]">{{ item.createTime }}</span>
            </div>
          </div>
          <div class="mt-20 flex" v-if="item.status === 2">
            <div
              class="w-160 rounded-40 text-26 leading-60 ml-auto h-60 bg-[#1E90FF] text-center text-white"
              @tap="payHandle(item.orderNo)"
            >
              立即支付
            </div>
          </div>
        </div>
      </div>
      <div v-if="orderList.length === 0" class="empty-container mt-240 mx-auto flex flex-col items-center">
        <div class="empty-img mb-40">
          <image class="h-240 w-330" src="../../static/images/jiaofei_empty.png" />
        </div>
        <div class="empty-text text-28 leading-36 text-[#909399]">暂无记录</div>
      </div>
      <div v-else class="empty-container text-28 leading-36 pb-20 text-center text-[#909399]">已加载全部记录</div>
    </div>
  </GlobalContainer>
</template>

<script setup>
import { ref, computed } from 'vue'
import { showLoading, hideLoading } from '@/utils'
import { statusMap } from '../baoxiuDetail/config'
import { getRepairOrderPage, paymentRepairOrder } from '@/common/api/report'

const OSS_URL = import.meta.env.VITE_OSS_URL

const headerRef = ref()
const headerHeight = computed(() => {
  return headerRef.value?.wrapStyle?.height
})
const typeMap = {
  0: '报修报事',
  1: '公共报事',
  2: '幸福随手拍'
}
// 状态 1=待收款 2=待分派 3=已派单 4=已关闭 5=已完成
const statusBarList = ref([
  {
    name: '全部',
    value: ''
  },
  {
    name: '待分配',
    value: 1
  },
  {
    name: '待支付',
    value: 2
  },
  {
    name: '已完成',
    value: 5
  }
])

const typeColorMap = {
  0: '#1E90FF',
  1: '#29CC96',
  2: '#667FFF'
}
const statusClassMap = {
  1: 'status-wait',
  2: 'status-waitpay',
  3: 'status-waitdone',
  4: 'status-waitdone',
  5: 'status-done',
  6: 'status-cancel'
}
const statusBarActive = ref('')

// const statusList = {
//   waitPay: {
//     name: '待支付'
//   },
//   waitSign: {
//     name: '待分配'
//   },
//   waitDone: {
//     name: '待完成'
//   },
//   done: {
//     name: '已完成'
//   }
// }
const statusBarChange = value => {
  statusBarActive.value = value
  getRepairOrderList(value)
}

const orderList = ref([])
const getRepairOrderList = status => {
  showLoading()
  getRepairOrderPage({
    pageNo: 1,
    pageSize: 9999,
    status
  })
    .then(res => {
      orderList.value = res.records || []
    })
    .finally(() => {
      hideLoading()
      uni.stopPullDownRefresh()
    })
}
// 超过一定字数截断
const cutText = text => {
  return text.length > 30 ? text.slice(0, 30) + '...' : text
}
getRepairOrderList(statusBarActive.value)
const toPageHandle = id => {
  uni.navigateTo({
    url: `/subPages/baoxiuDetail/index?id=${id}`
  })
}
const payHandle = orderNo => {
  paymentRepairOrder({ orderNo }).then(res => {
    // 拉起支付
    if (res.prepayId) {
      uni.requestPayment({
        provider: 'wxpay',
        timeStamp: String(res.timeStamp),
        nonceStr: res.nonceStr,
        package: 'prepay_id=' + res.prepayId,
        signType: 'SHA256withRSA',
        paySign: res.paySign,
        orderInfo: {
          appId: '',
          timeStamp: String(res.timeStamp),
          nonceStr: res.nonceStr,
          package: ''
        },
        success: function (res) {
          getRepairOrderList(statusBarActive.value)
        },
        complete: function () {}
      })
    }
  })
}
onPullDownRefresh(() => {
  getRepairOrderList(statusBarActive.value)
})
</script>

<style scoped lang="scss">
.status-bar-container {
  width: 702rpx;
  left: 50%;
  transform: translateX(-50%);
  height: 80rpx;
  line-height: 1;
  text-align: center;
  background: #ffffff;
  border-radius: 10rpx 10rpx 10rpx 10rpx;
  padding: 24rpx 0;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
}
.status-bar-item {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background-color: #fff;
  font-weight: 400;
  font-size: 28rpx;
  color: #606266;
  border-right: 1px solid #e3e7ec;
}
.status-bar-item-active {
  color: #1e90ff;
}

.list-c {
  .item-c {
    background: #fcfeff;
    border-radius: 20rpx 20rpx 20rpx 20rpx;
    border: 1rpx solid #ffffff;
    .line1,
    .line2 {
      display: flex;
      align-items: center;
    }
  }
}
.status {
  width: 118rpx;
  height: 52rpx;
  text-align: center;
  line-height: 52rpx;
  font-weight: 400;
  font-size: 26rpx;
  color: #ffffff;
  box-shadow: inset 0rpx 8rpx 16rpx 0rpx rgba(255, 255, 255, 0.3);
  border-radius: 12rpx 0rpx 12rpx 0rpx;
}
.status-wait {
  background: linear-gradient(303deg, #ff7733 0%, #ff4000 100%);
}
.status-waitdone {
  background: linear-gradient(303deg, #3389ff 0%, #339aff 100%);
}
.status-waitpay {
  background: linear-gradient(303deg, #ff9933 0%, #ff8000 100%);
}
.status-done {
  background: linear-gradient(303deg, #a3b4cc 0%, #8fa1b2 100%);
}
.status-cancel {
  background: linear-gradient(303deg, #a3b4cc 0%, #8fa1b2 100%);
}
.text-wrap {
  word-break: break-all;
}
</style>
